<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>javascript 阶段</title>
    <style>
        button {
            width: 150px;
            height: 50px;
            cursor: pointer;
            font-size: 20px;
        }
        .box {
            margin-top: 10px;
            width: 100px;
            height: 100px;
            background-color: #F00;
        }
        body {
            margin: 0;
        }
    </style>
</head>
<body>
    <button>往右走 <span></span> 步</button>
    <div class="box"></div>

    <script>
        // 要求：
            // 在页面上，每次点击按钮，就移动一下红色盒子，步长50
            // 目标位置是1000,需要点击多少次按钮，红色盒子方可到达目标位置

            var divItems = document.querySelector("button");
            var span = document.querySelector("button > span");
            var child = document.querySelector(".box");
            console.log(child.offsetLeft);
            var index = 0 ;
            var count = 0;
            divItems.onclick = function () {
                index ++ ;
                count += 50 ; 
                child.style["marginLeft"] = count + "px";
                // child.offsetLeft = count + "px";
                // console.log(child.offsetLeft);
                console.log(count);
                span.innerHTML = index ;
                console.log(index);
                if(count >= 1000)  {
                    count = 1000 ;
                }
            }
    </script>
</body>
</html>